function testTracking()

    setEnv();
    videoDir = 'C:\Users\rniehaus\Documents\Elegans\Videos';
    identifier = 'led_move1.avi';
    [video, nFrames, vidRows, vidCols] = loadVideo( 'AVI', videoDir, identifier );
     bwFrame = segWormVal(video(20).gdata);
     x = regionprops(bwFrame, 'Centroid');
     C = x(1).Centroid;
     %lastPosition = [round(C(2)), round(C(1))];
     lastPosition = [500,500];
        
     movie = [];
    
        
    for iFrame = 20:300
      
      [newRowPos, newColPos] = updatePosition(video(iFrame).gdata,lastPosition, 50);
       lastPosition = [newRowPos, newColPos];
       dotIndices = getDotIndices( [vidRows, vidCols], lastPosition, 5);
       newFrame = gray2rgb(video(iFrame).gdata);
       newFrame = applyOverlay(newFrame, dotIndices,'red'); 
      % newFrame =   video(iFrame).cdata;
        movie = cat(4, movie,newFrame);
       video(iFrame).gdata = [];
       video(iFrame).cdata = [];
    end
    
    implay(movie);
 

    
    function [newRowPosition, newColPosition] = updatePosition(imIn, lastPosition, window)
        
        rowPosition = lastPosition(1);
        colPosition = lastPosition(2);
        rowDot = 0;
        colDot = 0;
        sumIntensity = 0;
        rowMin = max(1, rowPosition - window);
        rowMax = min (size(imIn, 1), rowPosition + window);
        colMin = max(1, colPosition - window);
        colMax = min (size(imIn, 1), colPosition + window);
        imIn = imcomplement(imIn);
        for k = rowMin: rowMax
            for l = colMin: colMax
                rowDot = rowDot + k*double(imIn(k,l));
                colDot = colDot + l*double(imIn(k,l));
                sumIntensity = sumIntensity + double(imIn(k,l));
            end
        end

        newRowPosition = round(rowDot/(sumIntensity));
        newColPosition = round(colDot/(sumIntensity));
    end
end


